package com.finddeer.bookkeeping.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.BillLab;
import com.finddeer.bookkeeping.BillPagerActivity;
import com.finddeer.bookkeeping.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2019/1/2 7:10 PM
 * function  : 记账页面的内容Fragment，负责以可滚动列表的形式显示所有账单
 */
public class TodayFragment extends Fragment {


    private RecyclerView mBillRecyclerView;
    private BillAdapter mBillAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_today, container, false);
        ButterKnife.bind(this, view);

        mBillRecyclerView = view.findViewById(R.id.bill_recycler_view);
        mBillRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return view;
    }

    private void updateUI() {
        BillLab billLab = BillLab.get(getActivity());
        List<Bill> bills = billLab.getBills();
        Collections.sort(bills);    //  先按日期排序
        Collections.reverse(bills); //  再倒序排列，优先呈现日期近的
        if (mBillAdapter == null) {
            mBillAdapter = new BillAdapter(bills);
            mBillRecyclerView.setAdapter(mBillAdapter);
        } else {
            mBillAdapter.setBills(bills);
            mBillAdapter.notifyDataSetChanged();
        }
    }

    private class BillHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        private TextView mTitleTextView;
        private TextView mDetailsTextView;
        private TextView mAmountTextView;
        private TextView mPriceUnitTextView;

        private Bill mBill;

        public BillHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_bill, parent, false));
            mTitleTextView = itemView.findViewById(R.id.bill_title);
            mDetailsTextView = itemView.findViewById(R.id.bill_details);
            mAmountTextView = itemView.findViewById(R.id.bill_amount);
            mPriceUnitTextView = itemView.findViewById(R.id.bill_price_unit);
            itemView.setOnClickListener(this);
        }

        public void bind(Bill bill) {
            mBill = bill;

            mTitleTextView.setText(mBill.getTitle());

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd-kk:mm");
            mDetailsTextView.setText(simpleDateFormat.format(mBill.getDate()) + " " + mBill.getLocale() ); //  显示消费地点以及时间
            mAmountTextView.setText(mBill.getAmount()+"");
            mPriceUnitTextView.setText(R.string.price_unit);
        }


        @Override
        public void onClick(View v) {
            Intent intent = BillPagerActivity.newIntent(getActivity(), mBill.getId());
            startActivity(intent);
        }
    }

    private class BillAdapter extends RecyclerView.Adapter<BillHolder> {
        private List<Bill> mBills;

        public BillAdapter(List<Bill> bills) {
            mBills = bills;
        }

        @NonNull
        @Override
        public BillHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

            return new BillHolder(layoutInflater, parent);
        }

        @Override
        public void onBindViewHolder(@NonNull BillHolder holder, int position) {
            Bill bill = mBills.get(position);
            holder.bind(bill);
        }

        @Override
        public int getItemCount() {
            return mBills.size();
        }

        public void setBills(List<Bill> bills) {
            mBills = bills;
        }
    }

    @Override

    public void onResume() {
        super.onResume();
        updateUI();
    }

}
